<template>
	<view ref="main" v-if="mainloading" class="content">
	<u-popup :show="show" :round="10" mode="bottom" @close="close" >
		<view class="upop">
			 <view>本应用需要如下权限</view>
			 <view>蓝牙，定位</view>
			 <view class="botbar">
				 <view class="btn">
					 <u-button  type="success" @click="Linkblue"  text="我已知晓"></u-button>
				 </view>
			 </view>
 		</view>
	</u-popup>
	<u-swiper
            :list="list2"
            keyName="imageUrl"
            showTitle
            :autoplay="false"
            circular
			:height="225"
    ></u-swiper>
			<list
			@scrolltolower="reqtest"
			:showScrollbar="true"
			:offsetAccuracy="50000000000"
		>
			<list-item
				v-for="(item, index) in showList"
				:key="index"
			>
				
				<miantext :ref="item.id"  :key="item" :details="item" >
				</miantext>
			</list-item>
			</list>
			<view v-show="loading" class="centerloading">
				<u-loading-icon text="加载中" ></u-loading-icon>
			</view>
	</view>
	<view v-else>
	<u-skeleton
	    rows="13"
	    title
		loading
	></u-skeleton>
	</view>
</template>

<script>
import { unix } from '../../uni_modules/uview-ui/libs/util/dayjs';
   import miantext from "./components/miantext.vue"
	export default {
		components:{
			miantext
		},
		data() {
			return {
				title: 'Hello',
				 pageNum:1,
				 pageSize:5,
				 total:200,
				 list2:[],
				//   [{
                //     imageUrl: 'https://cdn.uviewui.com/uview/swiper/swiper2.png',
                //     title: '昨夜星辰昨夜风，画楼西畔桂堂东',
                // },{
                //     imageUrl: 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
                //     title: '身无彩凤双飞翼，心有灵犀一点通'
                // },{
                //     imageUrl: 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
                //     title: '谁念西风独自凉，萧萧黄叶闭疏窗，沉思往事立残阳'
                // }]
				mainloading:true,
				// let	blueList = [], servicesList = [], characteristicId = [];
				blueList:[],
				servicesList:[],
				characteristicId:[],
				show:true,
				savedFilePath:"",
				playLine:[],
				showList:[],
				loading:false,
				querytime:"",

			}
		},
		onLoad() {
					this.reqtest()
				this.requestImarg()
		},
		onShow(){
			// this.requestImarg()
			console.log("onshow--------------------------------------------------",uni.getStorageSync("maindata"));
			// if(uni.getStorageSync("maindata")){
			// 	console.log("onshow--------------------------------------------------");
			// 	let miandata = uni.getStorageSync("maindata")
			// 	this.pageNum = miandata.pageNum
			// 	this.pageSize = miandata.pageSize
			// 	this.total = miandata.total
			// 	this.showList = miandata.showList
			// 	this.list2 =  miandata.list2
			// }else{
			// 	this.reqtest()
			// 	this.requestImarg()
			// }
		
			this.$nextTick(()=>{
				// console.log("this.$refs[id]",this.$refs[1][0].likeNum);
				console.log("this.$showList",this.showList);
			})
			uni.$off("Listenersadd")
			uni.$on("Listenersadd",(id)=>{
				this.$nextTick(()=>{
					console.log("this.$refs[id]eeeeee",this.$refs[id][0].heardNumAdd);
					this.$refs[id][0].heardNumAdd()
				})
				
				})
			
		},
		onHide(){
			console.log("destroyed++++++++++++++++++++++++++++++",uni.getStorageSync("maindata"));
			let maindata = {}
				maindata.pageNum = this.pageNum
				maindata.pageSize = this.pageSize
				maindata.total = this.total
				maindata.showList = this.showList
				maindata.list2 =  this.list2
				uni.setStorageSync("maindata",maindata)
		},
		// onPullDownRefresh(){
		// 	// uni.stopPullDownRefresh({
		// 	// 	success:(res)=>{
		// 	// 	this.reqtest()
		// 	// 	}
		// 	// });
		// 	console.log(1);
		// 	this.reqtest()
		// },	
		onReachBottom(){
			console.log(1);
			this.reqtest()
		},
		methods: {
		 async	reqtest(){
			 	// 
				 console.log(111111);
				 if(this.loading)return
				 this.loading = true
				 if(this.showList.length>=this.total){
					  uni.showToast({
      					     icon: "none",
      					     title: "已经到最后了，暂无更多数据!",
      					     duration: 3000
      					 })
						this.loading = false
						return
					}
				 let num = `?pageSize=${this.pageSize}&pageNum=${this.pageNum++}`
			uni.request({
			    url: 'http://139.9.151.197:8889/app/home/article'+num, 
				method:"GET",
			    data: {
			    },
			    header: {
					Accept: "*/*"
			    },
			    success: (res) => {
					this.loading = false
					console.log(this.loading);
			        console.log(res.data);
					this.total = res.data.total
					console.log("this.total",this.total,this.pageSize*this.pageNum);
					for(let i = 0;i<res.data.rows.length;i++){
						res.data.rows[i].ids = (this.pageNum-2)*this.pageSize+i
						console.log("nanan",res.data.rows[i].ids);
					}
					if(this.total<=this.pageSize*this.pageNum){
						this.showList = this.showList.concat(res.data.rows)
						//  uni.showToast({
      					//      icon: "none",
      					//      title: "已经到最后了，暂无更多数据!",
      					//      duration: 3000
      					//  })
					}
					if(res.data.total>=this.pageSize*this.pageNum){
						this.showList = this.showList.concat(res.data.rows)
					}
					if(this.total == 0){
						this.showList = res.data.rows;
					}
					uni.$emit("Getlist",this.showList)
			    },
			});
			},
			async initplayTime(){
				 console.log("initplayTime",this.showList);
				for(let i=0;i<this.showList.length;i++){
				 this.showList[i].time = ""
				 console.log("initplayTime");
				 await uni.$emit("createInnerAudioContext_destroy")
				 await uni.$emit("createInnerAudioContext")
				 await uni.$emit("InnerAudioContext_init",{src:this.showList[i].audioUrl,autoplay:false,duration:this.showList[i].time})
				}
				
			},
			async Linkblue(){
				this.show = false
				uni.$emit("openBluetoothAdapter",{})
				uni.$emit("startBluetoothDevicesDiscovery",{})
				uni.$emit("onBluetoothDeviceFound",this.blueList)
				let a = setTimeout(
					()=>{
						uni.$emit("createBLEConnection",{blueList:this.blueList,servicesList:this.servicesList})
					}
				,1500)			
				},
			async requestImarg(){
				uni.request({
			    // url: 'http://192.168.31.18:8889/app/home/page-images', 
			    url: 'http://139.9.151.197:8889/app/home/page-images', 
				method:"GET",
			    data: {
			    },
			    header: {
					Accept: "*/*"
			    },
			    success: (res) => {
					console.log(res);
					this.list2 = res.data.data
					
			    },
			});
			},
			async Listenersadd(id){
					console.log("this.$refs[id]",this.$refs[id].heardNumAdd);
					this.$refs[0][id].heardNumAdd()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width:100%;
		text-align: center;
		overflow-y:hidden ;
	}
 .upop{
	 text-align: center;
 }
 .upop{
	 line-height: 2.5;
 }
 .botbar{
	 display: flex;
	 align-items: center;
	 height:50px;
 }
 .botbar .btn{
	 flex: 1;
	 width:30px;
	 min-width:30px  !important;
 }
 .centerloading{
	 text-align: center;
 }
 /deep/.u-button--small{
	 width:20px;
 }
 /deep/.u-button{
	 width:50%
 }
</style>
